package com.wzy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzy.mapper.VisitorMapper;
import com.wzy.pojo.Visitor;
import com.wzy.service.VisitorService;
import com.wzy.utils.JwtHelper;
import com.wzy.utils.R;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/visitor")
public class VisitorController {
    @Autowired
    JwtHelper jwtHelper;

    @Autowired
    VisitorMapper visitorMapper;

    @Autowired
    VisitorService visitorService;

    //来了一个访客插入数据库
    @GetMapping("/addVisitor")
    public R addVisitor(@RequestParam("ip") String ip, HttpServletRequest httpServletRequest) {
        //如果有ip地址相等的就更改最后访问时间
//        System.out.println(ip);//106.34.3.67 中国-河南省-郑州市-电信
        String flag = httpServletRequest.getHeader("User-Agent");//设备标识号

        String[] strs = ip.split(" ");
        String ipAddress = strs[0];
        String city = strs[1];
        //先根据ip查询
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("visitor_ip", ipAddress);
        Visitor visitor1 = visitorMapper.selectOne(queryWrapper);

        Visitor visitor = new Visitor();
        if (visitor1 != null) {
            //更新插入时间
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("visitor_ip", ipAddress);
            visitor.setLastAccessTime(new Date());//上一次登录时间
            visitorMapper.update(visitor, wrapper);
        } else {

            String c = "";
            if (flag.contains("Chrome")){
                c = "Chrome";
            }else if(flag.contains("Safari")){
                c = "Safari";
            }

            if (flag == null) {
                visitor.setUserAgent("未知设备 "+c);
            }else  if (flag.contains("Android")){
                visitor.setUserAgent("Android "+c);

            }else if(flag.contains("iPhone")){
                visitor.setUserAgent("iPhone "+c);

            }else if(flag.contains("iPad")){
                visitor.setUserAgent("iPad "+c);

            }else if(flag.contains("iOS")){
                visitor.setUserAgent("iOS "+ c);
            } else if(flag.contains("Windows")){
                visitor.setUserAgent("Windows "+ c);
            } else if(flag.contains("Mac")){
                visitor.setUserAgent("Mac "+c);
            }else {
                visitor.setUserAgent("未知设备 "+c);
            }
            visitor.setVisitorIp(ipAddress);
            visitor.setVisitorLocation(city);
            visitor.setFirstAccessTime(new Date());
            visitor.setLastAccessTime(new Date());
            visitorMapper.insert(visitor);
        }

        R ok = R.ok();
        ok.code(200);
        return ok;
    }

    //通过一个id列表删除列表
    @DeleteMapping("/deleteByIds")
    public R deleteByids(@RequestParam("ids") List<Integer> id, HttpServletRequest request) {
        String jwt = request.getHeader("jwt");
        if (jwtHelper.parseJwt(jwt)) {
            int count = 0;
            for (Integer i : id) {
                visitorService.deleteById(i);
                count++;
            }
            if (count == id.size()) {
                System.out.println("删除成功");
                System.out.println(count);
                System.out.println(id.size());
                R ok = R.ok();
                ok.code(200);
                return ok;
            } else {
//                System.out.println("删除出现异常");
//                System.out.println(count);
//                System.out.println(id.size());
                R error = R.error();
                error.code(501);
                return error;
            }
        } else {
            return R.error().code(300);
        }
    }

    //查看所有访客
    @GetMapping("/findAllVisitor")
    public R getAllVisitor(HttpServletRequest request) {
        String jwt = request.getHeader("jwt");
        if (jwtHelper.parseJwt(jwt)) {
            List<Visitor> allVisitor = visitorService.getAllVisitor();
            allVisitor.forEach(System.out::println);
            R ok = R.ok();
            ok.data("data", allVisitor);

            ok.code(200);
            return ok;
        } else {
            return R.error().code(300);
        }
    }

}
